package com.bls.cate.mvp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.bls.blslib.base.BasePresenter;
import com.bls.blslib.eventbus.Event;
import com.bls.blslib.eventbus.EventBusUtils;
import com.bls.blslib.view.LoadingDogDialog;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.ParameterizedType;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public abstract class MVPBaseFragment<V extends BaseView, T extends BasePresenterImpl<V>> extends BaseFragment implements BaseView {
    public T mPresenter;

    protected Context mContext;
    protected LoadingDogDialog loadingDogDialog;

    protected static final String ARG_PARAM1 = "param1";
    protected static final String ARG_PARAM2 = "param2";
    protected String mParam1;
    protected String mParam2;

/*    @Override
    @SuppressWarnings("all")
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);
    }*/

    @Override
    @SuppressWarnings("all")
    protected void initRoot() {
        mPresenter = getInstance(this, 1);
        mPresenter.attachView((V) this);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        if (isRegisterEventBus()) {
            EventBusUtils.register(this);
        }
        loadingDogDialog = new LoadingDogDialog(mContext);
    }

    @Override
    protected void initParams() {

    }

    @Override
    protected int initLayoutId() {
        return 0;
    }

    @Override
    protected void initFvb(View fragView) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOnHiddenChanged(boolean hidden) {

    }

    @Override
    protected void initOnPause() {

    }

    @Override
    protected void initOnResume() {

    }

    @Override
    protected void initOnDestroyView() {

    }

    @Override
    protected void initOnDestroy() {
        if (mPresenter != null)
            mPresenter.detachView();
    }


    /*    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }*/

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }

    @SuppressWarnings("all")
    public <T> T getInstance(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassCastException | java.lang.InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onRequestNetResult(int requestId, RequestResultState requestNetState, String responseBody, Response response) {
        if (requestNetState == RequestResultState.onStart && isOpenLoadingDogDialog())
            loadingDogDialog.show();
        if (requestNetState == RequestResultState.onFinish && isOpenLoadingDogDialog())
            loadingDogDialog.hide();
    }

    /**
     * 是否开启统一的  网络请求Loading
     *
     * @return 默认开启
     */
    protected boolean isOpenLoadingDogDialog() {
        return true;
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusCome(Event event) {
        if (event != null) {
            receiveEvent(event);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onStickyEventBusCome(Event event) {
        if (event != null) {
            receiveStickyEvent(event);
        }
    }

    /**
     * 接收到分发到事件
     *
     * @param event 事件
     */
    protected void receiveEvent(Event event) {

    }

    /**
     * 接受到分发的粘性事件
     *
     * @param event 粘性事件
     */
    protected void receiveStickyEvent(Event event) {

    }
}
